<!DOCTYPE html>
<html>
<head>
  <title>The Food Rescue Robot</title>
  <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.5.1/build/cssreset/cssreset-min.css">
  <link href='http://fonts.googleapis.com/css?family=Droid+Sans' rel='stylesheet' type='text/css'>
  <link rel="shortcut icon" href="/favicon.ico" type="image/ico" />
  <%= stylesheet_link_tag "common", "devise", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= javascript_include_tag :highcharts  %>
  <%= csrf_meta_tags %>
</head>
<%
#Code for ruby that should be moved into a controller

list_of_all_pickups = []
total_number_pickups = 0
pickups_this_day = 0
pickups_this_week = 0
pickups_this_month = 0
pickups_this_year = 0

recipient_count = 0

list_of_donors = []
donor_count = 0

#Get the pickup stats
total_number_pickups = Log.count + Region.all.collect{ |r| r.prior_num_pickups }.compact.sum
date = Date.today
pickups_this_day = Log.count(:conditions => {:when => date})
pickups_this_week = Log.count(:conditions => {:when => date.at_beginning_of_week(start_day = :sunday)...date.at_end_of_week(start_day = :sunday)})
pickups_this_month = Log.count(:conditions => {:when => date.at_beginning_of_month()...date.at_end_of_month()})
pickups_this_year = Log.count(:conditions => {:when => date.at_beginning_of_year()...date.at_end_of_year()})
  
#Amount of food stats
starting_amount = Region.all.collect{ |r| r.prior_lbs_rescued }.compact.sum
running_total = starting_amount
current_month = date.month
current_year = date.year
current_day = date.day

food_per_day = []
food_per_month = [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
start_date = Date.new(2012, 6, 26)
range = Date.today - start_date
for i in 0..range
  start_date += 1
  total = Log.where(:when => start_date).where("weight IS NOT NULL").sum(:weight)
  if total != nil
    running_total += total
    food_per_month[start_date.month - 1] += total
  end
  food_per_day << running_total/1000.0
end

food_chart_day = LazyHighCharts::HighChart.new('area') do |f|
  f.options[:chart][:defaultSeriesType] = "area"
  f.options[:chart][:plotBackgroundColor] = nil
  f.options[:title][:text] = "Food Rescued Since June 26, 2012"
  f.options[:xAxis][:title] = {:enabled => true, :text => "Days Since 06/26/2012"}
  f.options[:yAxis][:title][:text] = "thousand lbs of food"
  f.plot_options(:area => {
    :pointStart => 0,
    :marker => {
      :enabled => false,
      :symbol => 'circle',
      :radius => 2,
      :states => {
        :hover => {
          :enabled => true
        }
      }
    }
  })
  f.series(
    :name => 'Pounds of Food Rescued',
    :data => food_per_day
  )
end

month_labels = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
for i in 1..current_month
  month_labels = month_labels.push(month_labels.shift)
  food_per_month = food_per_month.push(food_per_month.shift)
end

food_chart_month = LazyHighCharts::HighChart.new('column') do |f|
  f.options[:chart][:defaultSeriesType] = "column"
  f.options[:chart][:plotBackgroundColor] = nil
  f.options[:title][:text] = "Food Rescued By Month (since 6/26/2012)"
    f.options[:xAxis] = {
    :plot_bands => "none",
    :title=>{:text=>"Month"},
    :categories => month_labels}
  f.options[:yAxis][:title][:text] = "lbs of food"
  f.series(
    :name => 'Pounds of Food Rescued',
    :data => food_per_month
  )
end

#Volunteer info
number_of_volunteers = Volunteer.count()
largest_pickup = Log.where("logs.weight IS NOT NULL").order("weight DESC").first
pickups_today = Schedule.where(:day_of_week => date.wday)
  
#Recipient Stats
recipient_count = Location.count(:conditions => {:is_donor => false})
  
#Donor Stats
list_of_donors = Location.where(:is_donor => true)
donor_count = list_of_donors.length

list_of_regions = Region.all

#Transportation Stats
total_trips = Log.count()
pie_data = []
human_count = 0
TransportType.all.each{ |tt|
  thecount = Log.count(:conditions => {:transport_type_id => tt.id})
  human_count += thecount if tt.name !~ /Car/i
  pie_data.push([tt.name,thecount])
}
nil_values = Log.count(:conditions => { :transport_type_id => nil })
human_power_percent = 100.0*human_count.to_f/(total_trips-nil_values).to_f

#Transporation Pie Chart
h = LazyHighCharts::HighChart.new('pie') do |f|
  f.chart({:defaultSeriesType => 'pie'})
  f.options[:chart][:renderTo] = "container"
  f.options[:chart][:plotBackgroundColor] = nil
  f.options[:title][:text] = "Transportation Type"
  f.plot_options(:pie => {
    :allowPointSelect=>true, 
    :cursor=>"pointer" , 
    :dataLabels=>{
      :enabled=>true,
      :color=>"black",
      :style=>{
        :font=>"13px Trebuchet MS, Verdana, sans-serif"
      }
    }
  })
  series = {
    :type => 'pie',
    :name => 'Tansportation Type',
    :data => pie_data
  }
  f.series(series)
end

%>
<body>
  <div class="container">
    <div class="header">
    <div>
      <h2 id="bfr_title">The Food Rescue Robot</h2>
      <h3>Just-in-Time Food Rescue Tracking</h3>
    </div>  
    <%= link_to image_tag("logo.png", :class => 'logo_image')%>
    </div>
    <div class="wrap">
    <div class="info_section">
      <h2 class="section_title"> About The Robot </h2>
      <p>
      The robot is a web-application for scheduling, routing, and tracking just-in-time food rescue.
      It was developed by volunteers of <a href="http://www.boulderfoodrescue.org">Boulder Food Rescue</a>,
      an all volunteer non-profit organization located in Boulder, Colorado. Although originally used exclusively
      by Boulder Food Rescue, the robot is now used by multiple organizations around the world, fighting malnutrition
      and food waste with love, community, and bicycles. If you'd like to start a food rescue group in your town, shoot
      an email to info at boulder food rescue dot org and we can set you up with an account on here. If you're a super-diy
      sort and want to hack on the source <a href="http://code.google.com/p/bfr-webapp/">you can do that too</a>.
      </p>

      <h2 class="section_title">Facts</h2>
      <div class="fact_section">
      <div class="a_fact_section">
        <p>Number of Cities Using the Robot: <%= list_of_regions.length %></p>
        <p class="show_recipient_facts text_link"> list </p>
                          <div class="fact_info donor_list">
                                <div class="fun_fact">
                                  <%
                                  list_of_regions.each do |r|
                                    if r.website != nil%>
                                      <div>
                                        <p class>
                                          <a href="<%= r.website %>"><%= r.title %></a>: <%= r.tagline %>
                                        </p>
                                      </div>
                                    <% else %>
                                      <div>
                                        <p> <%= r.name %>: <%= r.tagline %></p>
                                      </div>
                                    <% end %>
                                  <% end %>
                                </div>
                          </div>

      </div>
      <div class="a_fact_section">
        <p>Total Food Pickups To Date: <%= total_number_pickups %> pickups</p>
        <p class="show_pickups_to_date text_link"> fun facts </p>
        <div class="fact_info">
<!--          <div class="fun_fact">
            <h3>Pickups Today</h3>
            <div><p> <%= pickups_this_day %></p></div>
        </div>-->
        <div class="fun_fact">
          <h3>Pickups This Week</h3>
          <div>
            <p> <%= pickups_this_week %> </p>
          </div>
        </div>
        <div class="fun_fact">
          <h3>Pickups This Month</h3>
          <div>
            <p> <%= pickups_this_month %></p>
          </div>
        </div>
<!--        <div class="fun_fact">
          <h3>Pickups This Year</h3>
          <div>
            <p> <%= pickups_this_year %></p>
          </div>
        </div>-->
        </div>
      </div>
      <div class="a_fact_section">
        <p>Total Food Rescued: 
        <%= running_total.to_i %>
        lbs
        </p>
        <p class="food_pickup text_link"> chart </p>
        <div class="fact_info">
        <%= high_chart("food_chart_day", food_chart_day) %>
        <%= high_chart("food_chart_month", food_chart_month) %>
        </div>
      </div>
      <div class="a_fact_section">  
        <p>Number of Volunteers: <%= number_of_volunteers %> volunteers</p>
        <p class="volunteer_facts text_link"> fun facts </p>
        <div class="fact_info">
          <% if largest_pickup.volunteer != nil %>
            <div class="fun_fact">
              <h3>Largest Pickup</h3>
              <%
              donor = Location.find(largest_pickup.donor_id)
              if donor.website != nil %>
                <div>
                  <p>
                    <%= largest_pickup.volunteer.name %> with <%= largest_pickup.weight %> lbs of food from 
                    <a href="<% donor.website %>"><%= donor.name %></a>
                  </p>
                </div>
              <% else %>
                <div>
                  <p>
                    <%= largest_pickup.volunteer.name %> with <%= largest_pickup.weight %> lbs of food from <%= donor.name %>
                  </p>
                </div>
              <%end%>
          </div>
        <% end %>
        <div class="fun_fact">
          <h3>Volunteers Working Today</h3>
          <% 
          pickups_today.each do |pickup|
            next if pickup.volunteer.nil? or pickup.donor.nil?
              if donor.website != nil%>
                <div>
                  <p>
                    <%= pickup.volunteer.name %> - <a href="<%= donor.website %>"><%= donor.name %></a>
                  </p>
                </div>
            <% else %>
                <div>
                  <p>
                    <%= pickup.volunteer.name %> - <%= donor.name %>
                  </p>
                </div>
            <% end %>
          <% end %>
        </div>
        </div>
      </div>
      <div class="a_fact_section">
        <p>Number of Recipients: <%= recipient_count %></p>
        <p class="show_recipient_facts text_link"> list </p>
                          <div class="fact_info donor_list">
                                <div class="fun_fact">
                                  <h3>List of Recipients</h3>
                                  <%
                                  recips = Location.where(:is_donor => false)
                                  recips.each do |recip|
                                    next if recip.name =~ /Individuals/i
                                    if recip.website != nil%>
                                      <div>
                                        <p class>
                                          <a href="<%= recip.website %>"><%= recip.name %></a>
                                        </p>
                                      </div>
                                    <% else %>
                                      <div>
                                        <p> <%= recip.name %> </p>
                                      </div>
                                    <% end %>
                                  <% end %>
                                </div>
                          </div>

      </div>
      <div class="a_fact_section">  
        <p>Number of Donors: <%= donor_count %></p>
        <p class="show_donor_list text_link">list</p>
        <div class="fact_info donor_list">
          <div class="fun_fact">
            <h3>List of Donors</h3>
          <% 
          donors = Location.where(:is_donor => true)
          donors.each do |donor|
            if donor.website != nil%>
              <div>
                <p class> 
                  <a href="<%= donor.website %>"><%= donor.name %></a>
                </p>
              </div>
            <% else %>
              <div> 
                <p> <%= donor.name %> </p>
              </div>
            <% end %>
          <% end %>
        </div>
        </div>
      </div>  
      <div class="a_fact_section">
        <p>Percent of Trips Human Powered: 
        <%= human_power_percent.round %> %
        </p>
        <p class="show_trans_chart text_link"> chart </p>
        <div class="fact_info transportation_chart">
        <%= high_chart("trans_chart", h) %>
        </div>
      </div>
      </div>
    </div>
    <div class="sign_in_section">
      <div>
      <%= yield %>
      </div>
    </div>
    </div>
    <div class="push"></div>
    <div class="footer">
    <p>Copyright (c) <a href="http://www.boulderfoodrescue.org">Boulder Food Rescue</a> 2012</p>
    </div>
  </div>
</body>
</html>
